﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Etalk.Library;
namespace EtalkClient
{
    public partial class Talk : ControlFace.IMForm
    {
        ControlFace.User  talkUser;
        public Talk()
        {
            InitializeComponent();
        }
        public Talk(ControlFace.User obj)
        {
            base.CloseBoxClick += new EventHandler(Talk_CloseBoxClick);
            base.MaximizeBoxClick += new EventHandler(Talk_MaximizeBoxClick);
            base.MinimizeBoxClick += new EventHandler(Talk_MinimizeBoxClick);
            talkUser = obj;
            this.Load += new EventHandler(Message_Load);
            InitializeComponent();
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            if(Client.ClientConn.Sock.Connected)
            {
            string msgFromat = "PRIV|{0}|{1}|{2}";
            string content = string.Format(msgFromat, ClientUser.LoginedUser.ID, talkUser.UserID, txtSendMsg.Text);
            int i = Etalk.Library.Comm.SendDataToClient(Client.ClientConn, content);

            this.rtbHistory.AppendText(Environment.NewLine + ClientUser.LoginedUser.UserName + "   " + DateTime.Today.ToLocalTime());
            this.rtbHistory.AppendText(Environment.NewLine+"  "+ txtSendMsg.Text);
                 txtSendMsg.Text = "";
            
            }
            else
            {
                MessageBox.Show("与服务器连接断开.请重新登录");
                 
            }
        }
        public void Message_Load(object sender, EventArgs e)
        {
            this.Text = string.Format("与 {0} 交谈中", talkUser.UserName);
            foreach (ControlFace.MessageDate item in talkUser.Messages)
            {
                this.rtbHistory.AppendText(Environment.NewLine+talkUser.UserName + "   " + item.MessageTime.ToString());
                this.rtbHistory.AppendText(Environment.NewLine+"  "+item.Content.ToString());
            }
            talkUser.Messages.Clear();
        }
        Point point = new Point(300, 400);
        private void Talk_MaximizeBoxClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
                this.Location = point;
            }
            else
            {
                point = this.Location;
                this.WindowState = FormWindowState.Maximized;
            }
        }
        private void Talk_MinimizeBoxClick(object sender, EventArgs e)
        {

            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Minimized;
        }
        private void Talk_CloseBoxClick(object sender, EventArgs e)
        {
            this.Close();
        }

   
      
            
    }
}
